package javaapplication8.Make;

// $Source: /h/u1/szamosi/repo/b07/examples/remake/SimpleRule.java,v $
// $Date: 2007/06/24 00:25:13 $
// $Revision: 1.1.1.1 $

import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

class SimpleRule extends Rule {

    // Construct an empty rule
    public SimpleRule() {
        simpleInit(); // FIXME
    }
  
    // Construct a new rule with the specified target, pre-requisites,
    // and actions
    public SimpleRule(String target, String[] prereqs, String[] actions) {
        simpleInit();
        fTarget = target;
        for (int i=0; i<prereqs.length; ++i) {
            fPrereqs.add(prereqs[i]);
        }
        for (int i=0; i<actions.length; ++i) {
            fActions.add(actions[i]);
        }
    }
  
    // Get this rule's target
    public String getTarget() {
        return fTarget;
    }
  
    // Set this rule's target
    public void setTarget(String target) {
        fTarget = target;
    }
  
    // Iterate through pre-requisites
    public Iterator getPrereqIter() {
        return fPrereqs.iterator();
    }
  
    // Add a pre-requisite
    public void addPrereq(String prereq) {
        fPrereqs.add(prereq);
    }
  
    // Iterate through actions
    public Iterator getActionIter() {
        return fActions.iterator();
    }
  
    // Add an action
    public void addAction(String action) {
        fActions.add(action);
    }
  
    // Convert to printable form
    public String toString() {
        String result = fTarget + " :";
        Iterator ip = this.getPrereqIter();
        while (ip.hasNext()) {
            result += " " + (String)ip.next();
        }
        Iterator ia = this.getActionIter();
        while (ia.hasNext()) {
            result += "\n.\t" + (String)ia.next();
        }
        return result;
    }

    //------------------------------------------------------------
  
    // Common initialization
    protected void simpleInit() {
        fTarget = null;
        fPrereqs = new HashSet();
        fActions = new ArrayList();
    }

    protected String    fTarget;        // Rule's target
    protected Set       fPrereqs;       // Rule's pre-requisites
    protected List      fActions;       // Rule's actions
}
